package com.doctor.windflower_doctor.activity;

import java.io.File;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.SystemApplication;
import com.doctor.windflower_doctor.actionBeen.DoctorAction;
import com.doctor.windflower_doctor.db.SharedPreferencesDB;
import com.doctor.windflower_doctor.http.callback.HttpRequestCallBack;
import com.doctor.windflower_doctor.http.parser.impl.JsonParser;
import com.doctor.windflower_doctor.http.request.HttpRequest;
import com.doctor.windflower_doctor.http.request.HttpRequestParams;
import com.doctor.windflower_doctor.http.response.HttpResponseInfo;
import com.doctor.windflower_doctor.util.ActivityStack;
import com.doctor.windflower_doctor.util.Constent;
import com.doctor.windflower_doctor.util.StringUtil;
import com.doctor.windflower_doctor.view.LoadingDialog;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * <p>
 * 功能
 * </p>
 * 
 * <p>
 * Copyright 北京亚普拓信息咨询服务有限公司 2014 All right reserved.
 * </p>
 * 
 * @author 皮勉 时间 2014年11月25日 上午11:57:09
 * @version 1.0 </br> 最后修改人 无
 * 
 */
public class DialogActivity extends BaseActivity implements OnClickListener, Constent {
	private TextView title;
	private Button cancle, submit;
	private SharedPreferencesDB sharedPreferncesDB;
	private LoadingDialog loadingDialog;

	@Override
	protected void getSaveData(Bundle savedInstanceState) {

	}

	@Override
	protected void processLogic() {

		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		cancle.setOnClickListener(this);
		submit.setOnClickListener(this);
	}

	@Override
	protected void findViews() {
		loadingDialog = new LoadingDialog(this);
		sharedPreferncesDB = SharedPreferencesDB.getInstance(this);
		title = (TextView) findViewById(R.id.title);
		title.setText("您的账号已经在其它设备上登录，如非本人操作，则密码可能泄露！");

		cancle = (Button) findViewById(R.id.cancle);
		submit = (Button) findViewById(R.id.submit);
		cancle.setText("退出");
		submit.setText("重新登录");
	}

	@Override
	public int getResLayoutId() {

		return R.layout.sure_cancle_pop;

	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		int keyCode = event.getKeyCode();
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0 && event.getAction() == KeyEvent.ACTION_DOWN) {
			ImageLoader.getInstance().clearDiskCache();
			ImageLoader.getInstance().clearMemoryCache();
			if (MainActivity.instance != null)
				MainActivity.instance.finish();
			SystemApplication.getInstance().stopSoket();
			finish();
			ActivityStack.getInstance().finishAllActivity();
			System.exit(0);
		}
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cancle:
			ImageLoader.getInstance().clearDiskCache();
			ImageLoader.getInstance().clearMemoryCache();
			finish();
			if (MainActivity.instance != null)
				MainActivity.instance.finish();
			SystemApplication.getInstance().stopSoket();
			ActivityStack.getInstance().finishAllActivity();
			System.exit(0);
			break;
		case R.id.submit:
			if (StringUtil.isEmail(sharedPreferncesDB.getUserName())) {
				phone_Emial_Login(0 + "");
			} else {
				phone_Emial_Login(1 + "");
			}
			break;
		}
	}

	/**
	 * <p>
	 * 功能 重新登录
	 * </p>
	 * 
	 * @author 皮勉 时间 2014年11月25日 下午1:39:54
	 * @param number
	 */
	private void phone_Emial_Login(String number) {
		HttpMethod requestMethod = HttpMethod.POST;
		HttpRequestCallBack<DoctorAction> requestCallBack = new HttpRequestCallBack<DoctorAction>(new JsonParser(), DoctorAction.class) {
			@Override
			protected void handleResult(HttpResponseInfo<DoctorAction> responseInfo) {
				if (responseInfo.success.success) {

					if (responseInfo.success.data.verify == 1) {
						SystemApplication.getInstance().rigistSoket();
						sharedPreferncesDB.setIsLogin(true);
						sharedPreferncesDB.setDocotorId(responseInfo.success.data.accountId);
						sharedPreferncesDB.setUserName(responseInfo.success.data.username);
						sharedPreferncesDB.setAccountName(responseInfo.success.data.accountName);
						sharedPreferncesDB.setSingleId(responseInfo.success.data.id);
						sharedPreferncesDB.putImage(responseInfo.success.data.imgUrl);
						startActivity(new Intent(DialogActivity.this, MainActivity.class));
						finish();
					} else if (responseInfo.success.data.verify == 0) {
						Toast.makeText(DialogActivity.this, "您还未认证", Toast.LENGTH_SHORT).show();
					} else if (responseInfo.success.data.verify == 2) {
						Toast.makeText(DialogActivity.this, "待认证", Toast.LENGTH_SHORT).show();
					} else {
						Toast.makeText(DialogActivity.this, "认证未通过", Toast.LENGTH_SHORT).show();
					}
				} else {
					Toast.makeText(DialogActivity.this, responseInfo.success.message, Toast.LENGTH_SHORT).show();
				}
				loadingDialog.close();
			}

			@Override
			protected void handleError(HttpException e, String msg) {

				Toast.makeText(DialogActivity.this, "请检查网络状态", Toast.LENGTH_SHORT).show();

				loadingDialog.close();
			}

			@Override
			public void onStart() {

				super.onStart();
				loadingDialog.show();
			}

		};

		HttpRequestParams params = new HttpRequestParams(requestMethod, URL + DOCTOR_LOGIN, requestCallBack);
		params.addBodyParameter("username", sharedPreferncesDB.getUserName());
		params.addBodyParameter("password", sharedPreferncesDB.getPassword());
		params.addBodyParameter("type", number);
		HttpRequest.send(params);
	}
}
